package com.example.li893.drag;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List<Item> mList;
    private LayoutInflater mInflater;

    public MyAdapter(Context context,List<Item> itemlist){
        mList=itemlist;
        mInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if(convertView == null){
            viewHolder=new ViewHolder();
            convertView=mInflater.inflate(R.layout.item,null);
            viewHolder.imageView=(ImageView)convertView.findViewById(R.id.picture);
            viewHolder.textView=(TextView)convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder); //通过setTag将convertView和viewHolder关联
        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }

        Item item=mList.get(position);

        viewHolder.imageView.setImageResource(item.ID);
        viewHolder.textView.setText(item.titleName);

        return convertView;
    }

    class ViewHolder{
        public ImageView imageView;
        public TextView textView;
    }
}
